package com.woniu40.petai_user.service;

import com.woniu40.petai_user.entity.Article;
import com.woniu40.petai_user.entity.Dynamic;
import com.woniu40.petai_user.entity.User;
import com.woniu40.petai_user.mapper.*;
import com.woniu40.petai_user.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class UserService {
    @Autowired
    UserMapper userMapper;
    @Autowired
    DynamicMapper dynamicMapper;
    @Autowired
    CommentMapper commentMapper;
    @Autowired
    AttentionMapper attentionMapper;
    @Autowired
    CollectMapper collectMapper ;


    public User findUserByName(String userName) {
        return userMapper.selectUserByName(userName);
    }

    public int modifyErrorCount(String username){
        return userMapper.updateLoginErrorCount(username);
    }

    public  int modifyStatus(String username){
        return  userMapper.updateStatus(username);
    }

    public int  addUser(User user){
        return userMapper.insert(user);
    }

    public int modifyErrorCount0(String username){
        return userMapper.updateErrorCount(username);
    }


    public List<User> findAttentionUsers(Integer userId) {
        List<User> users = userMapper.selectUsers(userId);
        return users;
    }
    public User findUserById(Integer id){
        return userMapper.selectByPrimaryKey(id);
    }

    public int modifyUser(User user){
        return userMapper.updateByPrimaryKey(user.getUserName(),user.getPassword(),user.getUserPhone(),user.getUserEmail(),user.getSex(),user.getAddress(),user.getAvatar(),user.getIsmember(),user.getUserSign(),user.getUserId(),user.getUserStatus(),user.getErrorCount());


    }

    public int modifyAvatar(Integer userId,String avatar){
        int i = userMapper.updateAvatar(userId, avatar);
        return i;
    }

    @Transactional
    public UserVo findUserByUsericon(Integer userId) {
        UserVo userVo = userMapper.selectUserByUserId(userId);
        List<Dynamic> dynamicListList = dynamicMapper.selectDynamicByUserId(userVo.getUserId());
        List<User> attentionUsers = attentionMapper.selectAllAttionUserByUserid(userVo.getUserId());
        List<Article> articleList = collectMapper.selectCollectArticle(userVo.getUserId());
        userVo.setUserDynamicList(dynamicListList);
        userVo.setAttentionUserList(attentionUsers);
        userVo.setCollectList(articleList);
        return userVo;
    }
}







